<?php
namespace app\index\controller;

use think\Db;


class Excels 
{
   

      public function outExcelRecharge() {

        $data=Db::table('cadillace')->where("car","in",input('get.name'))->order('id desc')->select();



      // echo"<pre>"; var_dump($data);die();

        $field = array(
            'A' => array('id', 'ID'),
            'B' => array('car', '所选车型'),
            'C' => array('provide', '省份'),
            'D' => array('city', '城市'),
            'E' => array('gc', '购车时间'),
            'F' => array('name', '客户名字'),
            'G' => array('tel', '电话'),
            'H' => array('cw', '性别'),
            'I' => array('utime', '提交时间'),
        );
        $this->phpExcelList($field, $data, '凯迪拉克购车用户_' . date('Y-m-d'));
    }

    /**
     * 直接导出需要生产的内容
     * @param $field
     * @param $list
     * @param string $title
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function phpExcelList($field, $list, $title='文件')
    {
        import('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
      //  $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //设置保存版本格式旧版本
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); //新版本
        foreach ($list as $key => $value) {

            switch($value['cw']){
                case 1:
                $value['cw']="男";
                break;
                case 0:
                $value['cw']="女";
                break;
            }

            $value['utime']=date("Y-m-d H:i:s",$value['utime']);




            foreach ($field as $k => $v) {
                if ($key == 0) {
                    $objPHPExcel->getActiveSheet()->setCellValue($k . '1', $v[1]);
                }
                $i = $key + 2; //表格是从2开始的
                $objPHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
            }
        }
        header("Pragma: public");
        header("Expires: 0");
       // header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header('Cache-Control: max-age=0');
        header("Content-Type:application/force-download");
       // header("Content-Type:application/vnd.ms-execl");//xls版本
        header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//xlsx
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$title.'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        exit;
    }





}
